package cn.myfirefly.moming.portal.service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.myfirefly.moming.portal.dao.RoleSysDao;
import cn.myfirefly.moming.portal.dao.SystemAccessDao;
import cn.myfirefly.moming.portal.dao.UserSysDao;
import cn.myfirefly.moming.portal.entity.MMSystemAccess;
import cn.myfirefly.moming.portal.model.SystemAccessDto;
import cn.myfirefly.moming.portal.model.SystemAccessFilterDto;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.utils.BeanUtil;
import cn.myfirefly.utils.CollectionUtil;
import cn.myfirefly.utils.PropUtil;
import cn.myfirefly.utils.StrUtil;

@Service
@Transactional
public class SystemAccessService {

	@Autowired
	private SystemAccessDao systemAccessDao;
	
	@Autowired
	private RoleSysDao roleSysDao;
	
	@Autowired
	private UserSysDao userSysDao;
	
	public PageResultDto<MMSystemAccess> getSystemAccessList(SystemAccessFilterDto filterDto) {
		return systemAccessDao.getSystemAccessList(filterDto);
	}
	
	public List<MMSystemAccess> getSystemAccessListByRoleSn(String roles) {
		List<MMSystemAccess> accesses = roleSysDao.getSystemAccessListByRoleSn(roles);
		return accesses;
	}
	
	public List<Integer> getSysIdsByUserSn(Integer userSn) {
		List<Integer> l = new ArrayList<Integer>();
		List<Object> obj = userSysDao.getSysIdsByUserSn(userSn);
		if(obj != null && obj.size() > 0){
			for (Object object : obj) {
				l.add(Integer.valueOf(object.toString()));
			}
		}
		return l;
	}
	
	public String saveOrUpdate(SystemAccessDto dto) {
		//如果 systemid为1 不允许添加 1为系统内部portal用到了
		if(dto.getSystemId() == 1){
			return "系统编号已被系统内部使用";
		}

		MMSystemAccess access = null;
		if (dto.getSystemAccessId() == null) {
			// 说明是新增
			access = new MMSystemAccess();
			// 判断SystemId是否已经存在
			List<MMSystemAccess> list = systemAccessDao.getListBySysId(dto.getSystemId());
			if (CollectionUtil.isNotEmpty(list)) {
				return "系统编号已被使用，请重新设置系统编号";
			}
		} else {
			// 判断SystemId是否已经存在
			List<MMSystemAccess> list = systemAccessDao.getListBySysId(dto.getSystemId());
			if (CollectionUtil.isNotEmpty(list)) {
				for (MMSystemAccess mmSystemAccess : list) {
					if (!mmSystemAccess.getSystemAccessId().equals(dto.getSystemAccessId())) {
						if (dto.getSystemId().equals(mmSystemAccess.getSystemId())) {
							return "系统编号已被使用，请重新设置系统编号";
						}
					}
				}
			}
			
			// 说明是更新
			access = systemAccessDao.get(dto.getSystemAccessId());
			
		}
		
		BeanUtil.copyBeans(dto, access);
		access.setShowStatus('0');
		Integer sn = systemAccessDao.save(access);
		dto.setSystemAccessId(sn);
		return Const.ResultCode.SUCCESS;
	}
	
	public String changeStatus(Integer sn, Character state) {
		MMSystemAccess systemAccess = systemAccessDao.get(sn);
		if (systemAccess == null) {
			return "未找到系统模块";
		}
		
		systemAccess.setShowStatus(state);
		systemAccessDao.save(systemAccess);
		return Const.ResultCode.SUCCESS;
	}
	
	public String delete(Integer sn) {
		String realPath = PropUtil.use(Const.Config.CONFIG_FILE).get(Const.Config.PIC_REAL_PATH);
		String showPath = PropUtil.use(Const.Config.CONFIG_FILE).get(Const.Config.PIC_URL);
		//判断是否可以被删除
		long count = roleSysDao.countBySysId(sn);
		if(count > 0){
			 return "系统正在被使用，不能删除，如要删除，请先停用，再清除所有权限设置";
		 }
		//删除记录 1 先查出记录 2 删除记录 3 删除图片 
		SystemAccessDto dto = new SystemAccessDto();
		MMSystemAccess access = systemAccessDao.get(sn);
		systemAccessDao.del(access);
		
		BeanUtil.copyBeans(access, dto);
		
		//删除图片
		File file = null;
		if(StrUtil.notBlank(dto.getIconBig())){
			file = new File(dto.getIconBig().replaceAll(showPath, realPath));
			if(file.exists() && file.isFile()){
				file.delete();
			}
		}
		if(StrUtil.notBlank(dto.getIconSmall())){
			file = new File(dto.getIconSmall().replaceAll(showPath, realPath));
			if(file.exists() && file.isFile()){
				file.delete();
			}
		}
		return Const.ResultCode.SUCCESS;
	}
	
	public MMSystemAccess getSystemAccess(Integer sn) {
		return systemAccessDao.get(sn);
	}
	
	public MMSystemAccess getSystemAccessBySysId(Integer sysId) {
		return systemAccessDao.getBySysId(sysId);
	}
	
	public List<MMSystemAccess> getSystemAccessList(boolean isAll) {
		if (isAll) {
			return systemAccessDao.getAllList();
		}
		return systemAccessDao.getSystemAccessList();
	}
	
	public List<MMSystemAccess> getSystemAccessList(Character permissionType) {
		
		return systemAccessDao.getSystemAccessList(permissionType);
	}
}
